<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar
        title="搜索结果"
        left-arrow
        @click-left="$router.go(-1)"
        fixed
      />
    </div>
    <div>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <article-item
          v-for="obj in articleList"
          :key="obj.art_id"
          :articleObj="obj"
          :isShow="false"
          @click.native="detailFn(obj.art_id)"
        ></article-item>
      </van-list>
    </div>
  </div>
</template>

<script>
import { getSearchResultAPI } from '@/api'
import ArticleItem from '@/components/ArticleItem.vue'
export default {
  name: 'SearchResult',
  data () {
    return {
      articleList: [],
      loading: false,
      finished: false,
      page: 1
    }
  },

  created () {
    this.getSearchResultFn()
  },
  components: {
    ArticleItem
  },
  methods: {
    async getSearchResultFn () {
      const res = await getSearchResultAPI({
        page: this.page,
        per_page: 10,
        q: this.$route.query.q
      })
      console.log(res)
      if (this.data?.data?.results === null) {
        this.finished = true
      } else {
        this.articleList = [...this.articleList, ...res.data.data.results]
        this.loading = false
      }
    },
    onLoad () {
      this.page++
      this.getSearchResultFn()
    },
    detailFn (artId) {
      this.$router.push({
        path: '/article_detail',
        query: {
          artId
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
